<?php
/**
* TopSites - Компонент каталога ссылок для Joostina CMS.
* @author:      Лаборатория сайтов, http://sitelab.net.ru
* @version:     1.9
* @release:     2010.02.07
* @package:     com_topsites
* @copyright:   (C) 2010 http://sitelab.net.ru,  see /administrator/components/com_topsites/copyright.php
* @license:     http://www.gnu.org/copyleft/gpl.html GNU/GPL
**/
    defined( '_VALID_MOS' ) or die( '' ); 
	
function resizepng($imgnamesrc,$imgnamedest,$percent,$resample) {
    $im = @imagecreatefrompng ($imgnamesrc); /* Attempt to open */
    if (!$im) { /* See if it failed */
        $im2  = imagecreate (150, 30); /* Create a blank image */
        $bgc = imagecolorallocate ($im2, 255, 255, 255);
        $tc  = imagecolorallocate ($im2, 0, 0, 0);
        imagefilledrectangle ($im2, 0, 0, 150, 30, $bgc);
        /* Output an errmsg */
        imagestring ($im2, 1, 5, 5, "Error loading $imgname", $tc);
    }
        else {
                $im2=imagecreatetruecolor(imagesx($im)*$percent/100,imagesy($im)*$percent/100);
                if ($resample) imagecopyresampled($im2,$im,0,0,0,0,imagesx($im2),imagesy($im2),imagesx($im),imagesy($im));
                else imagecopyresized($im2,$im,0,0,0,0,imagesx($im2),imagesy($im2),imagesx($im),imagesy($im));
                imagepng($im2,$imgnamedest);
        }
    return $im2;
}

function resizegif($imgnamesrc,$imgnamedest,$percent,$resample) {
    $im = @imagecreatefromgif ($imgnamesrc); /* Attempt to open */
    if (!$im) { /* See if it failed */
        $im2  = imagecreate (150, 30); /* Create a blank image */
        $bgc = imagecolorallocate ($im2, 255, 255, 255);
        $tc  = imagecolorallocate ($im2, 0, 0, 0);
        imagefilledrectangle ($im2, 0, 0, 150, 30, $bgc);
        /* Output an errmsg */
        imagestring ($im2, 1, 5, 5, "Error loading $imgname", $tc);
    }
        else {
                $im2=imagecreatetruecolor(imagesx($im)*$percent/100,imagesy($im)*$percent/100);
                if ($resample) imagecopyresampled($im2,$im,0,0,0,0,imagesx($im2),imagesy($im2),imagesx($im),imagesy($im));
                else imagecopyresized($im2,$im,0,0,0,0,imagesx($im2),imagesy($im2),imagesx($im),imagesy($im));
                imagepng($im2,$imgnamedest);
        }
    return $im2;
}

function make_img_from_png($imgnamesrc) {
// vraжa image objekt na osnovu zadanog fajla
    $im_created = @imagecreatefrompng ($imgnamesrc); /* Attempt to open */
    if (!$im_created) { /* See if it failed */
        $im_resulted  = imagecreate (150, 30); /* Create a blank image */
        $bgc = imagecolorallocate ($im_resulted, 255, 255, 255);
        $tc  = imagecolorallocate ($im_resulted, 0, 0, 0);
        imagefilledrectangle ($im_resulted, 0, 0, 150, 30, $bgc);
        /* Output an errmsg */
        imagestring ($im_resulted, 1, 5, 5, "Error loading $imgname", $tc);
    }
        else {
                $im_resulted=imagecreatetruecolor(imagesx($im_created),imagesy($im_created));
        }
    return $im_resulted;
}

function make_img_from_gif($imgnamesrc) {
// vraжa image objekt na osnovu zadanog fajla
    $im_created = @imagecreatefromgif ($imgnamesrc); /* Attempt to open */
    if (!$im_created) { /* See if it failed */
        $im_resulted  = imagecreate (150, 30); /* Create a blank image */
        $bgc = imagecolorallocate ($im_resulted, 255, 255, 255);
        $tc  = imagecolorallocate ($im_resulted, 0, 0, 0);
        imagefilledrectangle ($im_resulted, 0, 0, 150, 30, $bgc);
        /* Output an errmsg */
        imagestring ($im_resulted, 1, 5, 5, "Error loading $imgname", $tc);
    }
        else {
                $im_resulted=imagecreatetruecolor(imagesx($im_created),imagesy($im_created));
        }
    return $im_resulted;
}


function resizepng_to_width($imgnamesrc,$imgnamedest,$width,$resample) {
    $im = @imagecreatefrompng ($imgnamesrc); /* Attempt to open */
    if (!$im) { /* See if it failed */
        $im2  = imagecreate (150, 30); /* Create a blank image */
        $bgc = imagecolorallocate ($im2, 255, 255, 255);
        $tc  = imagecolorallocate ($im2, 0, 0, 0);
        imagefilledrectangle ($im2, 0, 0, 150, 30, $bgc);
        /* Output an errmsg */
        imagestring ($im2, 1, 5, 5, "Error loading $imgname", $tc);
    }
        else {
                $im2=imagecreatetruecolor($width,imagesy($im)*$width/imagesx($im));
                if ($resample) imagecopyresampled($im2,$im,0,0,0,0,imagesx($im2),imagesy($im2),imagesx($im),imagesy($im));
                else imagecopyresized($im2,$im,0,0,0,0,imagesx($im2),imagesy($im2),imagesx($im),imagesy($im));
                imagepng($im2,$imgnamedest);
        }
    return $im2;
}

function resizegif_to_width($imgnamesrc,$imgnamedest,$width,$resample) {
    $im = @imagecreatefromgif ($imgnamesrc); /* Attempt to open */
    if (!$im) { /* See if it failed */
        $im2  = imagecreate (150, 30); /* Create a blank image */
        $bgc = imagecolorallocate ($im2, 255, 255, 255);
        $tc  = imagecolorallocate ($im2, 0, 0, 0);
        imagefilledrectangle ($im2, 0, 0, 150, 30, $bgc);
        /* Output an errmsg */
        imagestring ($im2, 1, 5, 5, "Error loading $imgname", $tc);
    }
        else {
                $im2=imagecreatetruecolor($width,imagesy($im)*$width/imagesx($im));
                if ($resample) imagecopyresampled($im2,$im,0,0,0,0,imagesx($im2),imagesy($im2),imagesx($im),imagesy($im));
                else imagecopyresized($im2,$im,0,0,0,0,imagesx($im2),imagesy($im2),imagesx($im),imagesy($im));
                imagepng($im2,$imgnamedest);
        }
    return $im2;
}

function resizepng_to_height($imgnamesrc,$imgnamedest,$height,$resample) {
    $im = @imagecreatefrompng ($imgnamesrc); /* Attempt to open */
    if (!$im) { /* See if it failed */
        $im2  = imagecreate (150, 30); /* Create a blank image */
        $bgc = imagecolorallocate ($im2, 255, 255, 255);
        $tc  = imagecolorallocate ($im2, 0, 0, 0);
        imagefilledrectangle ($im2, 0, 0, 150, 30, $bgc);
        /* Output an errmsg */
        imagestring ($im2, 1, 5, 5, "Error loading $imgname", $tc);
    }
        else {
                $im2=imagecreatetruecolor(imagesx($im)*$height/imagesy($im),$height);
                if ($resample) imagecopyresampled($im2,$im,0,0,0,0,imagesx($im2),imagesy($im2),imagesx($im),imagesy($im));
                else imagecopyresized($im2,$im,0,0,0,0,imagesx($im2),imagesy($im2),imagesx($im),imagesy($im));
                imagepng($im2,$imgnamedest);
        }
    return $im2;
}

function resizegif_to_height($imgnamesrc,$imgnamedest,$height,$resample) {
    $im = @imagecreatefromgif ($imgnamesrc); /* Attempt to open */
    if (!$im) { /* See if it failed */
        $im2  = imagecreate (150, 30); /* Create a blank image */
        $bgc = imagecolorallocate ($im2, 255, 255, 255);
        $tc  = imagecolorallocate ($im2, 0, 0, 0);
        imagefilledrectangle ($im2, 0, 0, 150, 30, $bgc);
        /* Output an errmsg */
        imagestring ($im2, 1, 5, 5, "Error loading $imgname", $tc);
    }
        else {
                $im2=imagecreatetruecolor(imagesx($im)*$height/imagesy($im),$height);
                if ($resample) imagecopyresampled($im2,$im,0,0,0,0,imagesx($im2),imagesy($im2),imagesx($im),imagesy($im));
                else imagecopyresized($im2,$im,0,0,0,0,imagesx($im2),imagesy($im2),imagesx($im),imagesy($im));
                imagepng($im2,$imgnamedest);
        }
    return $im2;
}

function resizepng_to_limit($imgnamesrc,$imgnamedest,$maxw,$maxh,$resample) {
// ako je slika 'imgnamesrc' po nekoj od dimezija veжa od limita (maxw,maxh) slika se limitira na taj limit,
// a druga dimenzija se odreрuje proporcionalno ('constrain')

  $im_resulted=make_img_from_png($imgnamesrc);
  $ww=imagesx($im_resulted);
  $hh=imagesy($im_resulted);
  if ($ww>$maxw) {
          $pomw=$maxw;
          $pomh=$hh*($maxw/$ww);
        $ww=$pomw;
        $hh=$pomh;
  }
  if ($hh>$maxh) {
          $pomh=$maxh;
          $pomw=$ww*($maxh/$hh);
        $ww=$pomw;
        $hh=$pomh;
  }
  if (!($ww===imagesx($im_resulted)))
          $imr=resizepng($imgnamesrc,$imgnamedest,100*$ww/imagesx($im_resulted),$resample);
  else $imr=resizepng($imgnamesrc,$imgnamedest,100,$resample);
  return $imr;
}

function resizegif_to_limit($imgnamesrc,$imgnamedest,$maxw,$maxh,$resample) {
// ako je slika 'imgnamesrc' po nekoj od dimezija veжa od limita (maxw,maxh) slika se limitira na taj limit,
// a druga dimenzija se odreрuje proporcionalno ('constrain')

  $im_resulted=make_img_from_gif($imgnamesrc);
  $ww=imagesx($im_resulted);
  $hh=imagesy($im_resulted);
  if ($ww>$maxw) {
          $pomw=$maxw;
          $pomh=$hh*($maxw/$ww);
        $ww=$pomw;
        $hh=$pomh;
  }
  if ($hh>$maxh) {
          $pomh=$maxh;
          $pomw=$ww*($maxh/$hh);
        $ww=$pomw;
        $hh=$pomh;
  }
  if (!($ww===imagesx($im_resulted)))
          $imr=resizegif($imgnamesrc,$imgnamedest,100*$ww/imagesx($im_resulted),$resample);
  else $imr=resizegif($imgnamesrc,$imgnamedest,100,$resample);
  return $imr;
}


function create_thumb_from_img($im,$imgnamedest,$xpos,$ypos,$width,$height,$thmbw,$thmbh,$resample) {
// kreira thumbnail tako da izreћe isjeиak zadanih dimenzija (width,height) sa zadane pozicije (xpos,ypos),
// smanji ga na dimenziju thumbnaila (thumbw,thumbh) i snimi u datoteku imgnamedest
        $im2=imagecreatetruecolor($thmbw,$thmbh);
        if ($resample) {
                $im3=imagecreatetruecolor($width,$height);
                imagecopyresized($im3,$im,0,0,$xpos,$ypos,$width,$height,$width,$height);
                imagecopyresampled($im2,$im3,0,0,0,0,$thmbw,$thmbh,$width,$height);
        }
        else imagecopyresized($im2,$im,0,0,$xpos,$ypos,$thmbw,$thmbh,$width,$height);
        imagepng($im2,$imgnamedest);
    return $im2;
}


?>
